// $Id: Example1.java,v 1.3 2002/02/18 15:33:00 pcharles Exp $

/***************************************************************************
 * Copyright (C) 2001, Patrick Charles and Jonas Lehmann                   *
 * Distributed under the Mozilla Public License                            *
 *   http://www.mozilla.org/NPL/MPL-1.1.txt                                *
 ***************************************************************************/

package org.demo.net.qsee;

import net.sourceforge.jpcap.capture.PacketCapture;
import net.sourceforge.jpcap.capture.RawPacketListener;
import net.sourceforge.jpcap.net.RawPacket;

/**
 * jpcap Tutorial - Example 1
 * 
 * @author Jonas Lehmann and Patrick Charles
 * @version $Revision: 1.3 $
 * @lastModifiedBy $Author: pcharles $
 * @lastModifiedAt $Date: 2002/02/18 15:33:00 $
 */
public class Example1 {
	private static final int INFINITE = -1;
	private static final int PACKET_COUNT = 10;

	// BPF filter for capturing any packet
	private static final String FILTER = "";

	private PacketCapture m_pcap;
	private String m_device;

	public Example1() throws Exception {
		// Step 1: Instantiate Capturing Engine
		m_pcap = new PacketCapture();

		// Step 2: Check for devices
		m_device = m_pcap.findDevice();
		
		// There is a bug in the open method when used on windows
		// You have to delete everything after a '\n'
		int idx = m_device.indexOf('\n');
		if (idx != -1){
			m_device = m_device.substring(0,idx);
		}		
		// Fixes windows problem with full name returned by findDevice().
		
		// Step 3: Open Device for Capturing (requires root)
		m_pcap.open(m_device, true);

		// Step 4: Add a BPF Filter (see tcpdump documentation)
		m_pcap.setFilter(FILTER, true);

		// Step 5: Register a Listener for Raw Packets
		m_pcap.addRawPacketListener(new RawPacketHandler());

		// Step 6: Capture Data (max. PACKET_COUNT packets)
		m_pcap.capture(INFINITE);
	}

	public static void main(String[] args) {
		try {
			Example1 example = new Example1();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}

class RawPacketHandler implements RawPacketListener {
	private static int m_counter = 0;

	public void rawPacketArrived(RawPacket data) {
		m_counter++;
		System.out.println("Packet " + m_counter + "\n" + data + "\n");
	}
}
